<?php
namespace app\know\validate;

use app\know\model\PaperCategoryModel;
use app\know\validate\KnowBaseValidate;

class PaperCategoryValidate extends KnowBaseValidate
{
    protected $rule = [
        'paper_uuid' => 'require|length:32',
		'name' => 'require|checkName',
        'type_code' => 'require',
        'uuid' => 'require',
    ];

    protected $message = [
        'chapter_uuid.require' => '章节ID为必填项',
        'chapter_uuid.length' => '章节ID不能为空',	
        'name.require' => '分类为必填项',	
        'type_code.require' => '分类code为必填项',
        'uuid.require' => 'uuid不能为空',
    ];

    protected $scene = [
        'add'  => ['paper_uuid', 'name', 'type_code'],
        'edit' => ['name', 'uuid'],
        'delete' => ['uuid'],
    ];

    protected function checkName($value, $rule, $data)
    {
        /*if ($this->currentScene === 'edit') 
        {
            return true;
        }*/

        $model = new PaperModel();
        $item  = $model->where('paper_uuid', $data['paper_uuid'])
					   ->where('name', $data['name'])
                       ->find();

        if (empty($item)) 
        {
            return true;
        } 
        else 
        {
            return $data['name'] . "---已经存在---";
        }
    }
    
}